<?php
function ajouter_donnees($table, $tab_name_form) {
    
    global $connexion_serveur;
    
    $sql = "";
    $sql_col = "(";
    $sql_val = "(";
    
    for($x=0; $x<count($tab_name_form); $x++) {
                
        $sql_col .= $tab_name_form[$x];
        $sql_val .= "'".$_POST[$tab_name_form[$x]]."'";
        
        if($x==(count($tab_name_form)-1)) {
            $sql_col .= ")";
            $sql_val .= ")";
        }
        else {
            $sql_col .= ", ";
            $sql_val .=", ";
        }
    }
    $sql = "INSERT INTO ".$table." ".$sql_col." VALUES ".$sql_val;
    
    $envoi_requete = mysqli_query($connexion_serveur, $sql);
    
    if($envoi_requete==true) {
        $message_action["texte"] = "Operation reussie !";
        $message_action["css_class"] = "alert-success";
    }
    else {
        $message_action["texte"] = "Une erreur est survenue pendant l'operation !";
        $message_action["css_class"] = "alert-danger";
    }
    return $message_action;
}

function modifier_donnees($table, $id_enregistrement, $tab_name_form) {
    
    global $connexion_serveur;
    
    $sql_col_val = "";
    $sql = "";
    
    for($x=0; $x<count($tab_name_form); $x++) {
                
        $sql_col_val .= $tab_name_form[$x]."='".$_POST[$tab_name_form[$x]]."'";
        
        if($x!=(count($tab_name_form)-1)) {
            $sql_col_val .= ", ";
        }
    }
   echo  $sql = "UPDATE ".$table." SET ".$sql_col_val." WHERE id=".$id_enregistrement;
    
    $envoi_requete = mysqli_query($connexion_serveur, $sql);
    
    if($envoi_requete==true) {
        $message_action["texte"] = "Operation reussie !";
        $message_action["css_class"] = "alert-success";
    }
    else {
        $message_action["texte"] = "Une erreur est survenue pendant l'operation !";
        $message_action["css_class"] = "alert-danger";
    }
    return $message_action;
}

function supprimer_donnees($table, $id_enregistrement) {
    
    global $connexion_serveur;
    
    $sql = "DELETE FROM ".$table." WHERE id=".$id_enregistrement;
    
    $envoi_requete = mysqli_query($connexion_serveur, $sql);
    
    if($envoi_requete==true) {
        $message_action["texte"] = "Operation reussie !";
        $message_action["css_class"] = "alert-success";
    }
    else {
        $message_action["texte"] = "Une erreur est survenue pendant l'op�ration !";
        $message_action["css_class"] = "alert-danger";
    }
    return $message_action;
}